
let user_info   = {}
let customer    = {}
let socket      = new WebSocket(window.socket_url + '?user_id=' + window.user_id + '&user_type=' + user_type)

Message.sendMsg(socket)
$('.person[data-chat=person1]').trigger('click')

// 消息处理
socket.onmessage = function (result) {
    let data = JSON.parse(result.data)
    console.log(data)

    // 消息发送接收成功
    if (data.code === 200) {
        let msg_type = data.msg_type
        switch (msg_type) {
            case 'online':
                // 上线返回基本数据信息
                let user_data = data.data

                // 设置客服名称
                $('.chat-box .current-chat-user-name .name').text(user_data.customer.username + ' 正在为您服务...')
                $('.conversation-start span').text(user_data.online_time)
                $('.person[data-chat=person1]').data('room_id', user_data.room_id)

                break
            case 'message':
                let msg_data = data.data
                Message.receiveMsg(msg_data.message)
                break
            default:
                Message.sweetTimer('未知的消息类型~')
        }
    } else {
        Message.sweetTimer(data.data.message)
    }
}

// 关闭连接
socket.onclose = function () {
    Message.sweetTimer('连接已关闭')
}

// 窗口关闭是 主动关闭服务
window.onbeforeunload = function () {
    socket.close()
}
